package cn.edu.hhu.DAO.impl;

import cn.edu.hhu.Bean.Student;
import cn.edu.hhu.Bean.Teacher;
import cn.edu.hhu.DAO.Teacher_infoDao;
import cn.edu.hhu.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @anthor: lddd
 * @date: 2020/5/16 - 22:50
 * @description:
 **/
public class Teacher_infoDaoImpl implements Teacher_infoDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 通过tid删除学生
     * @param tid
     * @return
     */
    @Override
    public boolean deleteTeacherByID(String tid) {
        try {
            String sql = "DELETE FROM teacher_info WHERE tid = ?";
            int update = template.update(sql, tid);
            if (update == 1) return true;
            else return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 注册一条教师信息
     * @param teacher
     * @return 是否成功
     */
    @Override
    public boolean insertSingleTeacher(Teacher teacher) {
        try {
            //activation默认为0
            String sql = "INSERT INTO teacher_info(tid,tname,activation) VALUES (?,?,0)";
            int update = template.update(sql, teacher.getTid(), teacher.getTname());
            if (update == 1) return true;
            else return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 批量注册多条教师信息
     * @param teachers
     */
    @Override
    public void batchInsertTeacher(List<Teacher> teachers) {
        //activation默认为0
        String sql = "INSERT IGNORE INTO teacher_info(tid,tname,activation) VALUES (?,?,0)";
        int[] ints = template.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                preparedStatement.setString(1, teachers.get(i).getTid());
                preparedStatement.setString(2, teachers.get(i).getTname());
            }

            @Override
            public int getBatchSize() {
                return teachers.size();
            }
        });
    }

    /**
     * 查询所有教师信息
     * @return
     */
    @Override
    public List<Teacher> findAllTeacher() {
        String sql = "SELECT * FROM teacher_info";
        List<Teacher> list = template.query(sql, new BeanPropertyRowMapper<>(Teacher.class));
        return list;
    }

    /**
     * 通过教师编号修改教师信息
     * @param teacher
     */
    @Override
    public void modifyTeacherByID(Teacher teacher) {
        String sql = "update teacher_info set department = ?  ,pwd = ? ,activation = ? where  tid = ?";
        int update = template.update(sql, teacher.getDepartment(), teacher.getPwd(), teacher.isActivation(), teacher.getTid());
    }

    /**
     * 设置教师编号为tid的账户密码为newPwd
     * @param tid
     * @param newPwd
     * @return
     */
    @Override
    public boolean changeTeacherPassword(String tid, String newPwd) {
        String sql = "update teacher_info set pwd = ?where  tid = ?";
        return template.update(sql, newPwd, tid) == 1;
    }

    @Test
    public void test1() {
        Teacher_infoDaoImpl teacher_infoDao = new Teacher_infoDaoImpl();
        teacher_infoDao.deleteTeacherByID("00000001");
    }

}
